package com.emotion.recognition.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EmotionRecognition implements EntryPoint {

	// One panel for each section.
	UploadPanel uploadPanel = new UploadPanel();
	EditPanel editPanel;
	AbsolutePanel resultsPanel;

	Panel currentPanel = Panel.UPLOAD;

	enum Panel {
		UPLOAD, EDIT;
	}

	private final ProcessImageServiceAsync processImageService = GWT
			.create(ProcessImageService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		Button continueButton = new Button("Continue");
		continueButton.setStyleName("continueButton");
		continueButton.addClickHandler(new ContinueClickHandler());

		// Add first panel to the root panel.
		RootPanel.get("panelContainer").add(uploadPanel);
		RootPanel.get("continueButtonContainer").add(continueButton);
	}

	class ContinueClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			if (currentPanel == Panel.UPLOAD) {
				// Upload Panel;
				uploadPanel.formPanel
						.addSubmitCompleteHandler(new SubmitCompleteHandler() {
							@Override
							public void onSubmitComplete(
									SubmitCompleteEvent event) {
								String results = event.getResults();
								if (!results.contains("500")) {
									// Upload was a success.
									currentPanel = Panel.EDIT;
									System.out.println("DEBUG: " + results);

									// Load new panel.
									editPanel = new EditPanel(results);
									RootPanel.get("panelContainer").remove(0);
									RootPanel.get("panelContainer").add(
											editPanel);
									editPanel.nextImage();
									enableJCrop();
								} else {
									Window.alert("Upload Fail");
								}
							}
						});
				uploadPanel.formPanel.submit();
			} else if (currentPanel == Panel.EDIT) {
				// Setup next image.
				if (editPanel.nextImage()) {
					enableJCrop();
				} else {
					// Gone through all the images
					String coordinates = getCoordsFromJS();
					System.out.println("DEBUG: " + coordinates);

					// Finished, send information to server.
					processImageService.processImages(coordinates,
							editPanel.getTestingEmotions(),
							new AsyncCallback<List<Double>>() {

								@Override
								public void onSuccess(List<Double> result) {
								}

								@Override
								public void onFailure(Throwable caught) {
									Window.alert("Error on processing image");
								}
							});
				}
			}
		}
	}

	native void enableJCrop() /*-{
		$wnd.initJCrop();
	}-*/;

	native String getCoordsFromJS() /*-{
		return $wnd.getCoords();
	}-*/;

}
